﻿using PureMVC.Interfaces;
using System;
using System.Collections.Generic;
using UnityEngine;
using WooEngine.UI.Base;
using WooEngine.Utility.Logger;

namespace WooEngine.UI.Config
{
    /// <summary>
    /// 页面状态
    /// </summary>
    public enum PageStatusType
    {
        None,
        Loading,
        Loaded,
        Inited,
        Opened,
        Closing,
        Closed,
    }

    /// <summary>
    /// 页面打开模式
    /// </summary>
    public enum PageModeType
    {
        Single = 0,     // 只存在一个
        SingleInStack,  // 只存在一个，并且进栈
        Multiple,       // 可以存在多个
    }

    /// <summary>
    /// 页面层级枚举
    /// </summary>
    public enum PageLayerType
    {
        UISystemLayer = 0,          // 用于系统及主菜单
        UIPageLayer = 1,            // 用于页面的层
        UITopWindowLayer = 2,             // 用于处于窗口上层的层
        UIGuideWinLayer = 3,        // 用于教学引导的层
        UIGuideWinTopLayer = 4,     // 用于教学引导不能阻挡的层
        UILoadPageLayer = 5,        // 用于游戏页面加载的层
        UITopLoadPageLayer = 6,     // 用于游戏页面加载的层上方需要弹出的页面
        UINetLoadingLayer = 7,      // 用于网络等待，不可操作时的loading层
    }

    

    /// <summary>
    /// 页面预制件信息
    /// </summary>
    public class UIBaseInfo
    {
        // 预制件名称
        public string PrefabName;
        // 脚本
        public Type PageClass;
        // 是否为创建后就不再销毁的页面
        public bool IsNeverDestoryPage;

        public Type[] Mediators;

        public UIBaseInfo(string prefabName, Type pageClass = null, bool isNeverDestoryPage = false, params Type[] mediators)
        {
            this.PrefabName = prefabName;
            this.PageClass = pageClass;
            this.IsNeverDestoryPage = isNeverDestoryPage;
            this.Mediators = mediators;
        }
    }

    /// <summary>
    /// 页面配置信息类，管理所有的页面信息注册记录
    /// </summary>
    public class PageConfig
    {
        /// <summary>
        /// 页面预制件信息管理字典
        /// string pageName
        /// </summary>
        private static Dictionary<string, UIBaseInfo> PageInfo_Dic = new Dictionary<string, UIBaseInfo>();

        /// <summary>
        /// 注册UI页面预制件信息，所有的c#页面在此进行管理
        /// </summary>
        public static bool RegistPageInfo(string pageName, UIBaseInfo baseInfo)
        {
            if (PageInfo_Dic.ContainsKey(pageName))
            {
                WooLogger.Error(StringUtils.AppendString("Regist PageInfo is Error! PageInfo_Dic.ContainsKey(",pageName,") =", PageInfo_Dic.ContainsKey(pageName).ToString()));
                return false;
            }
            PageInfo_Dic.Add(pageName, baseInfo);
            return true;
        }

        /// <summary>
        /// 移除不需要再使用的ui
        /// </summary>
        public static bool RemovePageInfo(string pageName)
        {
            if (!PageInfo_Dic.ContainsKey(pageName))
            {
                WooLogger.Error(StringUtils.AppendString("Remove PageInfo is Error! !PageInfo_Dic.ContainsKey(", pageName, ") =", PageInfo_Dic.ContainsKey(pageName).ToString()));
                return false;
            }
            return PageInfo_Dic.Remove(pageName);
        }


        public static UIBaseInfo GetPageInfo(string pageNmae)
        {
            return PageInfo_Dic.TryGetValue(pageNmae, out UIBaseInfo info) ? info : null;
        }


    }
}